package com.djf.djfcs.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;

/**
 * Canal客户端配置类
 */
@Slf4j
@Configuration
public class CanalConfig {
    
    @Autowired
    private CanalProperties canalProperties;
    
    /**
     * 创建Canal连接器
     * @return CanalConnector
     */
    @Bean
    public CanalConnector canalConnector() {
        log.info("初始化Canal连接器，服务器地址: {}:{}, 目标实例: {}", 
                canalProperties.getServer().getHost(), 
                canalProperties.getServer().getPort(), 
                canalProperties.getDestination());
        
        // 创建Canal连接器
        CanalConnector connector = CanalConnectors.newSingleConnector(
                new InetSocketAddress(canalProperties.getServer().getHost(), 
                                    canalProperties.getServer().getPort()),
                canalProperties.getDestination(),
                canalProperties.getUsername(),
                canalProperties.getPassword());
        
        return connector;
    }
}